package com.atguigu.study.base.interactive;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//v1
/*class AirCondition  //资源类（空调） OOP = Field + Method
{
    private int number = 0;

    public synchronized void increment()  //1.判断 2.干活 3.通知
    {
        //1.判断
       while (number != 0) {

            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //2.干活
        System.out.println(Thread.currentThread().getName() + "\t" + (++number));

        //3.通知
        this.notifyAll();
    }

    public synchronized void decrement() {
        //1.判断
        while (number == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //2.干活
        System.out.println(Thread.currentThread().getName() + "\t" + (--number));
        //3.通知
        this.notifyAll();
    }


}*/
//v2

class AirCondition {
    private int number = 0;
    final Lock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();

    public void increment() {
        //1.判断
        lock.lock();
        try {
            while (number!=0) {
                condition.await();
            }

            //2.干活
            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
            //3.通知
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();

        }
    }
    public  void decrement()  {
        //1.判断
        lock.lock();
        try {
             while (number==0) {
                condition.await();
            }
             //2.干活
            System.out.println(Thread.currentThread().getName()+"\t"+(--number));
             //3.通知
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {

            lock.unlock();

        }
    }
}

/**
 * @Author 快点干早点散
 * @Create 2025/4/22 22:09
 * Description:
 * 有一个初始值为零的变量，现有两个线程对该变量操作，
 *  * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
 *  3.虚假唤醒  wait方法 使用 while
 */
public class ProdConsumerDemo {
    public static void main(String[] args) {
        //1.new 资源类
        AirCondition airCondition = new AirCondition();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.increment();
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.decrement();
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.increment();
                try {
                    TimeUnit.MILLISECONDS.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                airCondition.decrement();
                try {
                    TimeUnit.MILLISECONDS.sleep(400);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();

    }
}
